<template>
  <el-dialog v-model="modelValue" title="添加课程" width="500px">
    <el-form :model="project" label-width="80px" ref="courseFormRef">
      <el-form-item
        label="课程名称"
        prop="name"
        :rules="{ required: true, message: '请输入课程名称' }"
      >
        <el-input
          v-model="project.name"
          placeholder="请输入课程名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="课件">
        <div
          v-if="project.file"
          class="flex items-center justify-between w-full"
        >
          <span>{{ project.file?.name }}</span>
          <el-button type="danger" link @click="project.file = undefined">
            删除
          </el-button>
        </div>
        <el-button v-else type="primary" @click="emits('upload')">
          上传课件
        </el-button>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="saveProject">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import type { FormInstance } from "element-plus";
import { ref } from "vue";

const modelValue = defineModel<boolean>("courseDialogVisible");
const project = defineModel<any>("project");

const emits = defineEmits<{
  upload: [];
  save: [];
  cancel: [];
}>();

const courseFormRef = ref<FormInstance>();

function saveProject() {
  courseFormRef.value?.validate((valid) => {
    if (valid) {
      emits("save");
    }
  });
}

function handleCancel() {
  emits("cancel");
}
</script>
<style scoped lang="scss"></style>
